package top.iflyoung.cim.biz.vo;

import lombok.Data;
import top.iflyoung.cim.biz.entity.ChatGroupMember;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author liguiyang
 * @description 分组用户ID
 * @sine 1.8
 * @date 2021/9/221:55
 **/
@Data
public class ChatGroupVO {
    private Long groupId;

    private Set<Long> memberIds;

    private ChatGroupVO() {
        this.memberIds = new HashSet<>();
    }

    /**
     * 将同组对象转换成树
     *
     * @param groupMemberList
     * @return
     * @throws IllegalAccessException
     * @throws NullPointerException
     */
    private static ChatGroupVO from(List<ChatGroupMember> groupMemberList) throws IllegalAccessException, NullPointerException {
        if (null == groupMemberList || groupMemberList.isEmpty()) {
            throw new NullPointerException("传入的参数为空");
        }
        ChatGroupVO result = new ChatGroupVO();
        result.setGroupId(groupMemberList.get(0).getGroupId());
        for (ChatGroupMember member : groupMemberList) {
            result.addMember(member);
        }
        return result;
    }

    private void addMember(ChatGroupMember member) throws IllegalAccessException {
        if (!this.getGroupId().equals(member.getGroupId())) {
            throw new IllegalAccessException("查询出来的成员不是同一组的");
        }
        this.memberIds.add(member.getMemberId());
    }

}
